package com.nsn.vs.sdm.component.svn.command;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.wc.SVNCommitClient;

import com.nsn.vs.sdm.component.svn.SvnClientManager;
import com.nsn.vs.sdm.component.svn.event.UpdateEventHandler;
import com.nsn.vs.sdm.util.string.UtilString;

/**
 * Used for executing update repository operation.
 * 
 * Available properties from operation are:
 *  revisionProperty
 * 
 * @author Jeccy.Zhao
 
 */
public class SvnCommandCommit extends SvnCommand 
{
	
	/**
	 * File items for add
	 */
	private File[] itemsForAdd;
	
	/**
	 * File items for removal
	 */
	private File[] itemsForDel;
	
	/**
	 * Committed comments
	 */
	private String comment;
	
	/**
	 * The sole constructor
	 * 
	 * @param manager
	 *         the svn client manager
	 * @param filesForAdd
	 *         the files for add
	 * @param filesForRemoval
	 *         the files for removal
	 * @param comment
	 */
	public SvnCommandCommit(SvnClientManager manager, 
			List<String> filesForAdd, List<String> filesForRemoval, String comment) 
	{
		super(manager);
		
		if (filesForAdd != null)
		{
			this.itemsForAdd = new File[filesForAdd.size()];
			
			for (int i = 0, size = filesForAdd.size(); i < size; i++)
			{
				this.itemsForAdd[i] = new File(UtilString.revertUnicodeChars(filesForAdd.get(i)));
			}
		}
		
		if (filesForRemoval != null)
		{
			this.itemsForDel = new File[filesForRemoval.size()];
			
			for (int i = 0, size = filesForRemoval.size(); i < size; i++)
			{
				this.itemsForDel[i] = new File(UtilString.revertUnicodeChars(filesForRemoval.get(i)));
			}
		}
		
		this.comment = comment;
		
		// sets the event handler for checkout operations
		manager.getManager().getCommitClient().setEventHandler(
				new UpdateEventHandler(this));
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.nsn.hzta.tcr.component.svn.Command#execute()
	 */
	@SuppressWarnings("deprecation")
	@Override
	public long execute() throws Exception 
	{
		
		List<File> paths = new ArrayList<File>();
		
		if (this.itemsForAdd != null)
		{
			for (File path : itemsForAdd)
			{
				try
				{
					this.svnClient.getManager().getWCClient().doAdd(
						path, this.force, false, false, this.recursive);
					
					paths.add(path);
				}
				catch (Exception e)
				{
					// do nothing
					e.printStackTrace();
				}
			}
		}
		
		if (this.itemsForDel != null)
		{
			for (File path : itemsForDel)
			{
				try
				{
					this.svnClient.getManager().getWCClient().doDelete(
						path, this.force, false);
					
					paths.add(path);
				}
				catch (Exception e)
				{
					// do nothing
				}
			}
		}
		
		// Get the Commit Client
		SVNCommitClient client = this.svnClient.getManager().getCommitClient();
		
		// Execute commit info
		SVNCommitInfo info = client.doCommit(
			paths.toArray(new File[paths.size()]), true, this.comment, null, null, 
			false, false, SVNDepth.fromRecurse(this.recursive));
		
		return info.getNewRevision();
	}

	@Override
	protected void validateAttributes() throws Exception 
	{
		
	}
}
